package com.gaogzhen.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaogzhen.domain.WorkIssue;
import com.gaogzhen.service.WorkIssueService;
import com.gaogzhen.utils.CurrentUser;
import com.gaogzhen.utils.R;
import com.gaogzhen.utils.SqlExecuteUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author Administrator
 * @version 1.0
 * @description 客服工单控制器
 * @date 2022-06-04 11:33
 */
@RestController
@RequestMapping("/workIssues")
@Api(tags = "客服工单控制器")
public class WorkIssueController {

    @Autowired
    private WorkIssueService workIssueService;

    @GetMapping
    @ApiOperation("分页查询客服工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页大小"),
            @ApiImplicitParam(name = "startTime", value = "客服工单起始时间"),
            @ApiImplicitParam(name = "endTime", value = "客服工单结束时间"),
            @ApiImplicitParam(name = "status", value = "客服工单状态"),
    })
    @PreAuthorize("hasAuthority('work_issue_query')")
    public R<Page<WorkIssue>> findByPage(@ApiIgnore Page<WorkIssue> page, String startTime, String endTime, Integer status) {
        return R.ok(workIssueService.findByPage(page, startTime, endTime, status));
    }

    @GetMapping("/info")
    @ApiOperation("查询客服工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "工单id")
    })
    @PreAuthorize("hasAuthority('work_issue_query')")
    public R<WorkIssue> info(String id) {
        return R.ok(workIssueService.info(id));
    }

    @PatchMapping
    @ApiOperation("更新客服工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workIssue", value = "客服工单")
    })
    @PreAuthorize("hasAuthority('work_issue_update')")
    public R update(@RequestBody @Validated WorkIssue workIssue) {
        workIssueService.updateWorkIssue(workIssue);
        return R.ok();
    }

    @GetMapping("/issueList")
    @ApiOperation("前台分页查询客服工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页大小"),
    })
    public R<Page<WorkIssue>> getIssueList(@ApiIgnore Page<WorkIssue> page) {
        return R.ok(workIssueService.getIssuesByUserId(CurrentUser.getCurrentUserId(), page));
    }

    @PostMapping("/addWorkIssue")
    @ApiOperation("添加客服工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workIssue", value = "workIssue json数据")
    })
    public R addWorkIssue(@RequestBody WorkIssue workIssue) {
        workIssue.setUserId(CurrentUser.getCurrentUserId());
        workIssue.setStatus(1);
        SqlExecuteUtils.sqlExceptionHandler(a -> workIssueService.save(a), workIssue, "添加工单失败");
        return R.ok();
    }
}
